let app = getApp();
// let access_token = wx.getStorageSync('access_token');
var http = {
  get:function(url,data,fn){
    if(url!='login'){
      data.access_token = wx.getStorageSync('access_token');
    }
    //加载model
    wx.showLoading({
      title: '加载中...',
    });
    console.log('请求数据',data);
    app.util.request({
      url: 'entry/wxapp/' + url,
      data: data,
      method: 'GET',
      success: function(res) {
        wx.hideLoading();
        if(res.data.errno==0){
          fn(res);
        } else {
          wx.showToast({
            title: res.message,
            duration: 2500,
            icon: 'none',
            mask: true,
            success: (res) => {},
            fail: (res) => {},
            complete: (res) => {},
          })
        }
      },
      fail: function (XMLHttpRequest) {
        wx.hideLoading();
        console.log(XMLHttpRequest);
        wx.showToast({
          title: XMLHttpRequest.data.message,
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {},
    })
  },
  post: function (url, data, fn) {
    if(url!='login'){
      data.access_token = wx.getStorageSync('access_token');
    }
    //加载model
    wx.showLoading({
      title: '加载中...',
    });
    console.log('请求数据',data);
    app.util.request({
      url: 'entry/wxapp/' + url,
      data: data,
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if(res.data.errno==0){
          fn(res);
        } else {
          wx.showToast({
            title: res.message,
            duration: 2500,
            icon: 'none',
            mask: true,
            success: (res) => {},
            fail: (res) => {},
            complete: (res) => {},
          })
        }
      },
      fail: function (XMLHttpRequest) {
        wx.hideLoading();
        console.log(XMLHttpRequest);
        wx.showToast({
          title: XMLHttpRequest.data.message,
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function (res) { },
    })
  },
}

module.exports = http;